package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.ProductDao;
import com.aaa.sbm.dao.StockOutApprovalDao;
import com.aaa.sbm.dao.StockOutDao;
import com.aaa.sbm.dao.StockOutDetailDao;
import com.aaa.sbm.entity.Product;
import com.aaa.sbm.entity.StockOut;
import com.aaa.sbm.entity.StockOutApproval;
import com.aaa.sbm.entity.StockOutDetail;
import com.aaa.sbm.service.StockOutApprovalService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StockOutApprovalServiceImpl implements StockOutApprovalService {

    @Resource
    private StockOutApprovalDao stockOutApprovalDao;

    @Resource
    private StockOutDetailDao stockOutDetailDao;

    @Resource
    private StockOutDao stockOutDao;

    @Resource
    private ProductDao productDao;

    @Override
    public Result getStockList(Page<StockOutApproval> stockOutApproval) {
        PageHelper.startPage(stockOutApproval.getPageNo(), stockOutApproval.getPageSize());
        List<StockOutApproval> stockOutApprovals = stockOutApprovalDao.selectList(stockOutApproval.getData());
        PageInfo<StockOutApproval> pageInfo = new PageInfo<>(stockOutApprovals);
        return ResultUtil.success(pageInfo);
    }

    @Override
    public Result updateStockIn(StockOutApproval stockOutApproval) {
        StockOutDetail stockOutDetail = new StockOutDetail();
        stockOutDetail.setId(stockOutApproval.getSdId());
        StockOutDetail stockOutDetail1 = stockOutDetailDao.selectById(stockOutApproval.getSdId());
        StockOut stockOut = new StockOut();
        stockOut.setId(stockOutDetail1.getOutId());
        stockOut.setStatus(0);
        int i = stockOutDao.updateStockOut(stockOut);
        Product product = productDao.selectById(stockOutDetail1.getProductId());
        int i1 = productDao.updateStock(stockOutDetail1.getProductId(), product.getStock()-stockOutDetail1.getQuantity());
        stockOutApproval.setStatus(1);
        int a = stockOutApprovalDao.updateStockOutApproval(stockOutApproval);
        return a + i>0?ResultUtil.success("审核通过") : ResultUtil.fail("系统错误");
    }

    @Override
    public Result updateStockIn2(StockOutApproval stockOutApproval) {
        StockOutDetail stockOutDetail = new StockOutDetail();
        stockOutDetail.setId(stockOutApproval.getSdId());
        StockOutDetail stockOutDetail1 = stockOutDetailDao.selectById(stockOutApproval.getSdId());
        StockOut stockOut = new StockOut();
        stockOut.setId(stockOutDetail1.getOutId());
        stockOut.setStatus(1);
        int i = stockOutDao.updateStockOut(stockOut);
        stockOutApproval.setStatus(1);
        int a = stockOutApprovalDao.updateStockOutApproval(stockOutApproval);
        return a + i>0?ResultUtil.success("审核通过") : ResultUtil.fail("系统错误");
    }
}
